// 提案审批和初查记录 proposal
import { formatDate } from '@/mixins/filters'
export default {
	data() {
		return {
			// 提案审批
			proposalTableData: {
				flag: false,
				total: 0,
				sizes: 10,
				size: 10,
				index: 1,
				data: [],
				ajax: true,  //是否要加载数据
				type: 1       //1.提案审批、2.初查记录
			},
			// 初查记录
			preliminaryTableData: {
				flag: false,
				total: 0,
				index: 1,
				data: []
			},
			proposalLayer: {
				show: false,
				title: ''
			}
		}
	},
	computed: {},
	mounted() { },
	methods: {
		// table每页条数改变
		proposalTableSizeChange(val) {
			if (this.proposalTableData.type == 1) {
				this.proposalTableData.size = val
				this.proposalTableData.flag = false
			}
			else if (this.proposalTableData.type == 2) {
				this.preliminaryTableData.size = val
				this.preliminaryTableData.flag = false
			}

			this.getProposalData(1)
		},
		// 提案审批列表
		getProposalData(index) {
			let _size = 0
			if (this.proposalTableData.type == 1) {
				this.proposalTableData.index = index
				_size = this.proposalTableData.size
			}
			else if (this.proposalTableData.type == 2) {
				this.preliminaryTableData.index = index
				_size = this.preliminaryTableData.size
			}
			let _data = {}
			if (this.caseDetail && this.caseDetail.caseInfo.id) {
				_data.caseInfoId = this.caseDetail.caseInfo.id
			}
			else {
				_data.clueInfoId = this.clueDetail.clueInfo.id
			}
			_data.dataType = this.proposalTableData.type
			_data.pageBean = {}
			_data.pageBean.pageIndex = index
			_data.pageBean.pageSize = _size
			api.caseRiskList(_data)
				.then(res => {
					res.list.map(item => {
						if (item.cdate) {
							item.cdate = formatDate(item.cdate)
						}
						if (item.riskType) {
							let _obj = this.dicts.riskType.find(o => o.value == item.riskType)
							if (_obj) {
								item.riskTypeName = _obj.name
							}
						}
						if (item.riskCategory) {
							let _obj = this.dicts.riskCategory.find(o => o.value == item.riskCategory)
							if (_obj) {
								item.riskCategoryName = _obj.name
							}
						}
						if (item.riskLevel) {
							let _obj = this.dicts.riskLevel.find(o => o.value == item.riskLevel)
							if (_obj) {
								item.riskLevelName = _obj.name
							}
						}
						if (item.riskDisposeDate) {
							let _obj = this.dicts.riskDisposeDate.find(o => o.value == item.riskDisposeDate)
							if (_obj) {
								item.riskDisposeDateName = _obj.name
							}
						}
						item.riskDetailsName = item.riskDetails
						// if (item.riskDetails) {
						// 	let _obj = this.dicts.riskDetails.find(o => o.value == item.riskDetails)
						// 	if (_obj) {
						// 		item.riskDetailsName = _obj.name
						// 	}
						// }
						if (item.adjunct) {
							item.adjunctName = item.adjunct.substring(item.adjunct.indexOf('&fileName=') + 10)
						}
						return item
					})
					if (this.proposalTableData.type == 1) {
						this.proposalTableData.data = []
						this.proposalTableData.data = res.list
						if (!this.proposalTableData.flag) {
							this.proposalTableData.total = res.totalCount
							this.proposalTableData.flag = true
						}
					}
					else if (this.proposalTableData.type == 2) {
						this.preliminaryTableData.data = []
						this.preliminaryTableData.data = res.list

						if (!this.preliminaryTableData.flag) {
							this.preliminaryTableData.total = res.totalCount
							this.preliminaryTableData.flag = true
						}
					}
				})
				.catch(res => {
					console.log(res)
				})
		},
		// 显示提案审批弹窗
		showProposalLayer() {
			this.initRiskData()
			this.proposalLayer.show = true
			if (this.proposalTableData.type == 1) {
				this.proposalLayer.title = '新增提案审批记录'
			}
			else if (this.proposalTableData.type == 2) {
				this.proposalLayer.title = '新增初查记录'
				if (this.uploadData && this.uploadData.fileList) {
					this.uploadData.fileList = []
				}
			}
		},
		// 保存提案审批 
		saveProposal() {
			if (!this.riskCategoryForm.id) {
				this.$msg.error('请选择风险类别')
				return false
			}
			if (!this.riskTypeForm.value) {
				this.$msg.error('请选择风险类型')
				return false
			}
			if (!this.riskLevelForm.value) {
				this.$msg.error('请选择风险等级')
				return false
			}
			if (this.proposalTableData.type == 1) {
				if (!this.riskDisposeDateForm.value) {
					this.$msg.error('请选择处理时长')
					return false
				}
			}
			if (!this.riskDetailsForm.value) {
				this.$msg.error('请选择详情描述')
				return false
			}
			let _data = {}
			if (this.riskCategoryForm.id) {
				let _category = this.dicts.riskCategory.find(item => item.id == this.riskCategoryForm.id)
				if (_category) {
					_data.riskCategory = _category.value
				}
			}
			_data.riskType = this.riskTypeForm.value
			_data.riskLevel = this.riskLevelForm.value
			if (this.proposalTableData.type == 1) {
				_data.riskDisposeDate = this.riskDisposeDateForm.value
			}
			_data.riskDetails = this.riskDetailsForm.value
			if (this.proposalTableData.ajax) {
				_data.dataType = this.proposalTableData.type

				if (this.caseDetail && this.caseDetail.caseInfo.id) {
					_data.caseInfoId = this.caseDetail.caseInfo.id
				}
				else {
					_data.clueInfoId = this.clueDetail.clueInfo.id
				}
				if (this.uploadData && this.uploadData.fileList && this.uploadData.fileList.length) {
					_data.adjunct = this.uploadData.fileList[0].url
				}
				api.addCaseRisk(_data)
					.then(res => {
						this.$msg.success('新增成功')
						this.proposalLayer.show = false
						if (this.proposalTableData.type == 1) {
							this.proposalTableData.flag = false
						}
						else if (this.proposalTableData.type == 2) {
							this.preliminaryTableData.flag = false
						}
						this.getProposalData(1)
					})
					.catch(res => {
						console.log(res)
					})

				if (typeof this.editCaseInfoVo === 'function') {
					this.editCaseInfoVo()
				}
			}
			else {
				_data.creator = this.userInfo.name
				_data.cdate = formatDate(Date.now())
				if (_data.riskType) {
					let _obj = this.dicts.riskType.find(o => o.value == _data.riskType)
					if (_obj) {
						_data.riskTypeName = _obj.name
					}
				}
				if (_data.riskCategory) {
					let _obj = this.dicts.riskCategory.find(o => o.value == _data.riskCategory)
					if (_obj) {
						_data.riskCategoryName = _obj.name
					}
				}
				if (_data.riskLevel) {
					let _obj = this.dicts.riskLevel.find(o => o.value == _data.riskLevel)
					if (_obj) {
						_data.riskLevelName = _obj.name
					}
				}
				if (_data.riskDisposeDate) {
					let _obj = this.dicts.riskDisposeDate.find(o => o.value == _data.riskDisposeDate)
					if (_obj) {
						_data.riskDisposeDateName = _obj.name
					}
				}

				_data.riskDetailsName = _data.riskDetails
				// if (_data.riskDetails) {
				// 	let _obj = this.dicts.riskDetails.find(o => o.value == _data.riskDetails)
				// 	if (_obj) {
				// 		_data.riskDetailsName = _obj.name
				// 	}
				// }
				this.proposalTableData.data.push(_data)
				this.proposalLayer.show = false
			}
		},
		// 删除提案审批
		delCaseRisk(id) {
			if (this.proposalTableData.ajax) {
				api.delCaseRisk({ id: id })
					.then(res => {
						this.$msg.success('删除成功')
						if (this.proposalTableData.type == 1) {
							this.proposalTableData.flag = false
						}
						else if (this.proposalTableData.type == 2) {
							this.preliminaryTableData.flag = false
						}
						this.getProposalData(1)
					})
					.catch(res => {
						console.log(res)
					})
			}
			else {
				this.proposalTableData.data.splice(id, 1)
			}
		}
	}
}